/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.easoftware.code.util;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.easoftware.code.error.EaError;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

/**
 * Implement a sample JDBCConnectionHandler for MySQL database
 *
 * @author <a href="http://www.mannhaupt.com/danko/contact/">Danko Mannhaupt</a>
 * @since 2.0
 * @version see jdbcappender.jar/META-INF/MANIFEST.MF for version information
 */
public class MySqlConnectionHandler {

    private static MySqlConnectionHandler me;
    public static final String USER_DATA = "USER_DATA";
    public static final String HOST = "DBHOST";

    public static MySqlConnectionHandler getMe() {
        if (me == null) {
            me = new MySqlConnectionHandler();
        }

        return me;
    }
    String url = "jdbc:mysql://localhost/logmanager?user=root&password=";
    String username = "root";
    String password = "eliueliu";

    static {
        try {
            Driver dMySql = (Driver) (Class.forName("com.mysql.jdbc.Driver").newInstance());
            DriverManager.registerDriver(dMySql);
        } catch (Exception e) {
            System.err.println("Could not register driver.");
        }
    }

    public Connection getConnection() {
        try {
            String host = System.getProperty(HOST);
            String[] usr = System.getProperty(USER_DATA).split(":");
            url = "jdbc:mysql://" + host + "/logmanager?user=" + usr[0] + "&password=" + (usr.length <= 1 ? "" : usr[1]);

            return getConnection(url, username, password);
        } catch (Exception ex) {
            Logger.getLogger(MySqlConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Connection getConnection(String _url, String _username, String _password) {
        Connection con = null;
        try {

            con = DriverManager.getConnection(url);
            con.setAutoCommit(true);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

        return con;
    }

    public List<Object[]> execSql(String sql) throws EaError {
        try {
            ResultSetHandler rh = new ArrayListHandler();
            QueryRunner r = new QueryRunner();
            return (List<Object[]>) r.query(getConnection(), sql, rh);
        } catch (SQLException ex) {
            Logger.getLogger(MySqlConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
            throw new EaError(username, ex);
        }
    }
}
